/*
 // C15商品详情页 20151026 FZH
 */
$(function() {
	commTemplUtil.initPage();
	getShop();
	//左下 的 一级分类 和 二级分类的切换
	$(".show-hidden").click(function() {
		var s = $(".tabl-c").css("display");
		if (s == "none") {
			$(".tabl-c").css("display", "block")
			$(".bg-add").css("background-position", "-313px -55px")
		} else {
			$(".bg-add").css("background-position", "-291px -55px")
			$(".tabl-c").css("display", "none")
		}
	});

	//商品系列的 切换
	$("#categorymenu li").click(function() {
		$("#categorymenu li").attr("class", "");
		$(this).attr("class", "current");
		var getId = $(this).children().attr("id");
		$(".default").attr("style", "display:none");
		$("." + getId + "").attr("style", "display:block");
	});
	//判断是否 从自由集散区 过来的 1 = 采购分销区

});

//商品 详情
function getShop() {
	var shopDetail = _.template($("#details-templ").html());
	var goods_id = $("#goods_id").val() || "";
	$.ajax({
		url: PAGE_CONFIG.requestUrl.shopDetails,
		type: "get",
		dataType: "json",
		data: {
			goods_id: goods_id,
		},
		success: function(result) {
			var datas = result.data;
			var shopTemp = shopDetail(datas);
			$("#shop_details").html(shopTemp);

			var getLoginHref = COMM_DATA.topObj.login_href;
			var getLogin = COMM_DATA.topObj.loginFlag;
			if (getLogin == "0") {
				$("#add_shopping_cart").click(function() {
					window.location = "" + getLoginHref + "";
				});
				//initialize(getLogin, getLoginHref);
				$("#release_consult").click(function() {
					window.location = "" + getLoginHref + "";
				});
				$("#buynow_submit").click(function() {
					window.location = "" + getLoginHref + "";
				});
				var specVal = $("#shop_spec > a").html();
				if (specVal == "") {
					$("#shop_spec").html([]);
				}
			} else {
				var getPrice = datas[0].price;
				if (getPrice == "未授权") {
					var specVal = $("#shop_spec > a").html();
					if (specVal == "") {
						$("#shop_spec").html([]);
					}
					$("#add_shopping_cart").click(function() {
						alert("该商品未授权，请申请授权");
					});
					$("#buynow_submit").click(function() {
						alert("该商品未授权，请申请授权");
					});
				} else {
					var specVal = $("#shop_spec > a").html();
					if (specVal == "") {
						$("#shop_spec").html([]);
					}
					addCart();
					buyshop();
					release_consult();
				}
			}
		},
		error: function(result) {
			$("#shop_details").html();
		},
	});
};
//加入 购物车
function addCart() {

	$("#add_shopping_cart").click(function() {
		var getStock = $("#stock_num").text();
		var getMin = $("#minimum_order").text();
		var shopId = $(".purchase-price").attr("code");
		var shopSpecId = $("#current").attr("specid");
		var buyNum = $("#buynum").val();
		// console.log("getMin："+getMin+ " buyNum:"+buyNum +" getStock");
		if (parseInt(getMin) <= parseInt(buyNum)) {
			if (parseInt(buyNum) <= parseInt(getStock)) {
				$.ajax({
					url: PAGE_CONFIG.requestUrl.addStoreCart,
					type: "post",
					dataType: "json",
					data: {
						shopId: shopId || "",
						shopSpecId: shopSpecId || "",
						buyNum: buyNum || "",
						_token: COMM_DATA.csrfToken,
					},
					success: function(result) {
						dialogFunc();
					},
					error: function(xhr, status, error) {
						alert("加入购物车失败,该商品可能已加入购物车！");
					},

				});
			} else {
				alert("请输入 正确的数量");
			}
		} else {
			alert("请输入 正确的数量");
		}
	});
};
//立即购买
function buyshop() {
	$("#buynow_submit").click(function() {
		var getStock = $("#stock_num").text();
		var getMin = $("#minimum_order").text();
		var buyNum = $("#buynum").val();
		if(parseInt(getMin) <= parseInt(buyNum)) {
			if (parseInt(buyNum) <= parseInt(getStock)) {
				var goodsId = $(this).attr("code");
				var buyNum = $("#buynum").val();
				var getToken = COMM_DATA.csrfToken;
				var shopSpecId = $("#shop_spec > #current").attr("specid") || '';
				var submitFrom = "<form method='post' action='/order/shopping-order'><input type='hidden' name='goodsId' value=" + goodsId + "><input type='hidden' name='buyNum' value="+buyNum +"><input type='hidden' name='_token' value="+getToken+"><input type='hidden' name='shopSpecId' value="+shopSpecId+"><input type='submit' id='submit-btn'><input type='hidden' name='buy' value='1'></form>";				$("#hidden_form").html(submitFrom);
				$("#submit-btn").click();
			} else {
				alert("请输入 正确的数量");
			}

		} else {
			alert("请输入 正确的数量");
		}
		//var getPrice = $(".purchase-price").html();

	});
};
//弹出框的加载
function dialogFunc() {
	var dialogTemp = _.template($('#addr-dialog-templ').html());
	var dia = dialog({
		width: "480",
		title: " ",
		content: dialogTemp
	});
	dia.showModal();
};
//发布咨询
function release_consult() {
	$("#release_consult").click(function() {
		var content = $("#textarea_content").val();
		var storeId = $(".purchase-price").attr("code");
		var cryptonym = $('input:radio[name="release"]:checked').val();
		var userId = COMM_DATA.topObj.userid || "";
		$.ajax({
			url: PAGE_CONFIG.requestUrl.releaseConsult,
			type: "post",
			data: {
				content: content || "",
				goodsId: storeId || "",
				cryptonym: cryptonym || "",
				userId: userId,
				_token: COMM_DATA.csrfToken,
			},
			dataType: "json",
			success: function(result) {
				alert("成功");
			},
			error: function() {
				alert("失败");
			}
		});
	});
}